package com.cwt_taxi_xa.util;

import com.cwt_taxi_xa.MainApplication;
import com.cwt_taxi_xa.R;
import com.cwt_taxi_xa.custom_view.MyToast;
import com.cwt_taxi_xa.service.SerialPortService;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class ToastUtils {

	private static ToastUtils instance = new ToastUtils();


	/**
	 *
	 * 实例化当前上下文对象
	 *
	 */
	public static ToastUtils getInstance() {
		if (instance == null) {
			instance = new ToastUtils();
		}
		return instance;
	}

	public static void showToast(final String s) {
		try {
			if(SerialPortService.handler!=null){
				SerialPortService.handler.post(new Runnable() {
					@Override
					public void run() {
						Toast.makeText(MainApplication.getInstance(), s,
								Toast.LENGTH_LONG).show();
					}
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static Toast customToast;
	public static View toastView;
	public static TextView textView;
	/**
	 * 自定义Toast
	 * @param s
	 */
	public static void showToastForCustom(final String s){
		try {
			if(MainApplication.topActivity == null){
				return;
			}
			SerialPortService.handler.post(new Runnable() {
				@Override
				public void run() {
					if(toastView != null && toastView.isShown()){
						customToast.cancel();
					}
					LayoutInflater inflater = MainApplication.topActivity.getLayoutInflater();
					toastView = inflater.inflate(R.layout.toast_layout, null);
					textView =(TextView)toastView.findViewById(R.id.toast_text);
					customToast = new Toast(MainApplication.topActivity);
					customToast.setView(toastView);
					customToast.setDuration(Toast.LENGTH_LONG);
					customToast.setGravity(Gravity.CENTER, 0, 130);
					textView.setText(s);
					customToast.show();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 自定义Toast
	 * @param resID
	 */
	public static void showToastForCustom(final int resID){
		try {
			if(MainApplication.topActivity == null){
				return;
			}
			SerialPortService.handler.post(new Runnable() {
				@Override
				public void run() {
					if(toastView != null && toastView.isShown()){
						customToast.cancel();
					}
					LayoutInflater inflater = MainApplication.topActivity.getLayoutInflater();
					toastView = inflater.inflate(R.layout.toast_layout, null);
					textView =(TextView)toastView.findViewById(R.id.toast_text);
					customToast = new Toast(MainApplication.topActivity);
					customToast.setView(toastView);
					customToast.setDuration(Toast.LENGTH_LONG);
					customToast.setGravity(Gravity.CENTER, 0, 130);
					textView.setText(MainApplication.topActivity.getResources().getString(resID));
					customToast.show();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 自定义Toast
	 * @param s
	 * @param color	#ffffff
	 */
	public static void showToastForCustom(final String s, final String color){
		try {
			if(MainApplication.topActivity == null){
				return;
			}
			SerialPortService.handler.post(new Runnable() {
				@Override
				public void run() {
					if(toastView != null && toastView.isShown()){
						customToast.cancel();
					}
					LayoutInflater inflater = MainApplication.topActivity.getLayoutInflater();
					toastView = inflater.inflate(R.layout.toast_layout, null);
					textView =(TextView)toastView.findViewById(R.id.toast_text);
					customToast = new Toast(MainApplication.topActivity);
					customToast.setView(toastView);
					customToast.setDuration(Toast.LENGTH_LONG);
					customToast.setGravity(Gravity.CENTER, 0, 130);
					textView.setText(s);
					textView.setTextColor(Color.parseColor(color));
					customToast.show();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 自定义Toast
	 * @param resID
	 */
	public static void showToastForCustom(final int resID, final String color){
		try {
			if(MainApplication.topActivity == null){
				return;
			}
			String text = MainApplication.topActivity.getResources().getString(resID);
			showToastForCustom(text, color);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取版本名称
	 *
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public String getVersionName(Context context) throws Exception {
		PackageManager packageManager = context.getPackageManager();
		PackageInfo packInfo = packageManager.getPackageInfo(
				context.getPackageName(), 0);
		return packInfo.versionName;
	}

	private Dialog currentDlg;

	/**
	 * 查询等待消息提示框
	 *
	 * @param mcontext
	 * @param msgid
	 * @return
	 */
	public void popProgress(final Context context, final int msgid) {
		SerialPortService.handler.post(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				Resources res = context.getResources();
				String msg = res.getString(msgid);
				ProgressDialog pd = new ProgressDialog(context);
				pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
				currentDlg = ProgressDialog.show(context, null, msg);
				currentDlg.setCancelable(true);
				currentDlg.setCanceledOnTouchOutside(false);
			}
		});
		//		return currentDlg;
	}

	/**
	 * 查询等待消息提示框
	 *
	 * @param mcontext
	 * @param msgid
	 * @return
	 */
	public void popProgress(final Context context, final String msg) {
		try {
			SerialPortService.handler.post(new Runnable() {

				@Override
				public void run() {
					Resources res = context.getResources();
					ProgressDialog pd = new ProgressDialog(context);
					pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
					currentDlg = ProgressDialog.show(context, null, msg);
					currentDlg.setCancelable(true);
					currentDlg.setCanceledOnTouchOutside(false);
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭查询等待消息提示框
	 *
	 * @param mcontext
	 * @param msgid
	 * @return
	 */
	public boolean isCurrentDlgShowing() {
		return currentDlg != null && currentDlg.isShowing();
	}

	/**
	 * 关闭查询等待消息提示框
	 *
	 * @param mcontext
	 * @param msgid
	 * @return
	 */
	public void dismissCurrentDlg() {
		if (isCurrentDlgShowing()) {
			currentDlg.dismiss();
		}
	}

	/********************************************************************************/

	/**
	 * 自定义Toast
	 * @param s
	 */
	public static void showToastForMessage(final String s){
		try {
			SerialPortService.handler.post(new Runnable() {
				@Override
				public void run() {
					MyToast messageToast = new MyToast(MainApplication.getInstance().getApplicationContext());
					messageToast.setText(s);
					messageToast.setGravity(Gravity.BOTTOM, 0, 0);
					messageToast.show();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
			AppLog.e(ExceptionUtil.getInfo(e), e);
		}
	}

	/**
	 *
	 * 检测网络是否连接
	 *
	 */
	public static boolean isConnect(Context context) {
		boolean flag = false;
		try {
			if(context != null){
				ConnectivityManager connectivity = (ConnectivityManager) context
						.getSystemService(Context.CONNECTIVITY_SERVICE);
				if (connectivity != null) {
					NetworkInfo info = connectivity.getActiveNetworkInfo();
					if (null != info && info.isConnected()) {
						if (info.getState() == NetworkInfo.State.CONNECTED) {
							flag = true;
						}
					}
				}
			}
		} catch (Exception e) {
			AppLog.e(ExceptionUtil.getInfo(e), e);
			e.printStackTrace();
		}
		return flag;
	}
}
